﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Backend.Packets
{
    public class AccountPacket : IPacket
    {
        public enum AccountCommand
        {
            ACCOUNT_LOGIN_REQUEST,
            ACCOUNT_LOGOUT_REQUEST,
            ACCOUNT_LOGIN_RESPONSE,
            ACCOUNT_NEW_ACCOUNT_REQUEST
        }

        private AccountCommand m_Command;

        private const short m_nPacketId = 1;

        private byte[] m_Data;

        private short m_nLength;

        private string m_sLogin;
        private string m_sPassword;

        private string m_sResponse;

        public string Response
        {
            get
            {
                return m_sResponse;
            }
            set
            {
                m_sResponse = value;
            }
        }

        public string Login
        {
            get
            {
                return m_sLogin;
            }
            set
            {
                m_sLogin = value;
            }
        }

        public string Password
        {
            get
            {
                return m_sPassword;
            }

            set
            {
                m_sPassword = value;
            }
        }

        public AccountCommand PacketCommand
        {
            get
            {
                return m_Command;
            }

            set
            {
                m_Command = value;
            }
        }

        public byte[] Pack()
        {
            switch (m_Command)
            {
                case AccountCommand.ACCOUNT_LOGIN_REQUEST:
                    Data = Encoding.ASCII.GetBytes(Login + "," + Password);
                    Length = (short)(Data.Length + 10);
                    break;
                case AccountCommand.ACCOUNT_LOGIN_RESPONSE:
                    Data = Encoding.ASCII.GetBytes(Response);
                    Length = (short)(Data.Length + 10);
                    break;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter br = new BinaryWriter(ms);

                br.Write((short)Length);

                br.Write((short)m_nPacketId);

                br.Write((short)m_Command);

                ms.Position += 4;

                br.Write(Data);

                ms.Capacity = Data.Length + 10;

                return ms.GetBuffer();
            }
        }

        public void Load(byte[] Buffer)
        {
            using (MemoryStream ms = new MemoryStream(Buffer))
            {
                BinaryReader br = new BinaryReader(ms);

                m_nLength = br.ReadInt16();

                ms.Position += 2;

                m_Command = (AccountCommand)br.ReadInt16();

                ms.Position += 4;

                m_Data = br.ReadBytes(m_nLength - 10);
            }

            switch (m_Command)
            {
                case AccountCommand.ACCOUNT_LOGIN_REQUEST:
                    {
                        string[] sData = Encoding.ASCII.GetString(Data).Split(',');
                        Login = sData[0];
                        Password = sData[1];
                        break;
                    }
                case AccountCommand.ACCOUNT_LOGIN_RESPONSE:
                    {
                        Response = Encoding.ASCII.GetString(Data);
                        break;
                    }
            }
        }

        public byte[] Data
        {
            get
            {
                return m_Data;
            }
            set
            {
                m_Data = value;
            }
        }

        public short Length
        {
            get
            {
                return m_nLength;
            }
            set
            {
                m_nLength = value;
            }
        }
    }
}
